package com.yuan.model_payment.service.impl;

import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_payment.domain.PaymentBill;
import com.yuan.model_payment.repository.PaymentBillDao;
import com.yuan.model_payment.service.PaymentBillService;
import com.yuan.model_payment.service.dto.PaymentBillDto;
import com.yuan.model_payment.service.dto.PaymentBillQueryCriteria;
import com.yuan.model_payment.service.mapstruct.PaymentBillMapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;


@Service
@RequiredArgsConstructor
public class PaymentBillServiceImpl implements PaymentBillService {

    private final PaymentBillDao paymentBillDao;

    private final PaymentBillMapper paymentBillMapper;

    @Override
    public PageResult<PaymentBillDto> queryAll(PaymentBillQueryCriteria criteria, Pageable pageable) {
        Page<PaymentBill> page = paymentBillDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(paymentBillMapper::toDto));
    }

    @Override
    public List<PaymentBillDto> queryAll(CommonQueryCriteria criteria) {
        List<PaymentBill> users = paymentBillDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        return paymentBillMapper.toDto(users);
    }

    @Override
    public void download(List<PaymentBillDto> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PaymentBillDto paymentBillDto : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PaymentBill resources) {
        paymentBillDao.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PaymentBill resources) throws Exception {
//        PaymentBill paymentBill = paymentBillDao.findById(resources.getPayId()).orElseGet(PaymentBill::new);
        Optional<PaymentBill> byId = paymentBillDao.findById(resources.getPayId());
        if (byId.isPresent()) {
//            BeanUtils.copyProperties(resources, byId.get());
            byId.get().setPayStatus(resources.getPayStatus());
            paymentBillDao.save(byId.get());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer ids) {
        paymentBillDao.deleteById(ids);
    }
}
